package com.example.demotest.service;

import com.example.demotest.pojo.User;
import com.example.demotest.pojo.UserFollow;
import com.example.demotest.repository.UserFollowRepository;
import com.example.demotest.repository.UserRepository;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class UserFollowService {

    private final UserRepository userRepository;
    private final UserFollowRepository userFollowRepository;

    public UserFollowService(UserRepository userRepository, UserFollowRepository userFollowRepository) {
        this.userRepository = userRepository;
        this.userFollowRepository = userFollowRepository;
    }

    @Transactional
    public boolean follow(Integer myId, Integer targetId) {
        if (myId.equals(targetId)) {
            throw new IllegalArgumentException("不能关注自己");
        }

        User me = userRepository.findById(myId)
                .orElseThrow(() -> new IllegalArgumentException("当前用户不存在"));
        User target = userRepository.findById(targetId)
                .orElseThrow(() -> new IllegalArgumentException("目标用户不存在"));

        boolean exists = userFollowRepository.findByFollowerAndFollowing(me, target).isPresent();
        if (exists) {
            return false; // 已经关注过
        }

        UserFollow follow = new UserFollow();
        follow.setFollower(me);
        follow.setFollowing(target);
        userFollowRepository.save(follow);
        return true;
    }

    @Transactional
    public boolean unfollow(Integer myId, Integer targetId) {
        User me = userRepository.findById(myId)
                .orElseThrow(() -> new IllegalArgumentException("当前用户不存在"));
        User target = userRepository.findById(targetId)
                .orElseThrow(() -> new IllegalArgumentException("目标用户不存在"));

        Optional<UserFollow> followOpt = userFollowRepository.findByFollowerAndFollowing(me, target);
        if (followOpt.isPresent()) {
            userFollowRepository.delete(followOpt.get());
            return true;
        } else {
            return false; // 没有关注过
        }
    }


    public List<User> getFollowing(Integer userId) {
        User me = userRepository.findById(userId).orElseThrow();
        return userFollowRepository.findByFollower(me)
                .stream()
                .map(UserFollow::getFollowing)
                .collect(Collectors.toList());
    }

    public List<User> getFollowers(Integer userId) {
        User me = userRepository.findById(userId).orElseThrow();
        return userFollowRepository.findByFollowing(me)
                .stream()
                .map(UserFollow::getFollower)
                .collect(Collectors.toList());
    }

    public boolean isFollowing(Integer myId, Integer targetId) {
        User me = userRepository.findById(myId).orElseThrow();
        User target = userRepository.findById(targetId).orElseThrow();

        return userFollowRepository.findByFollowerAndFollowing(me, target).isPresent();
    }
}